<template>
  <div>
    <el-form :inline="true" :model="state.formInline" class="demo-form-inline">
      <el-form-item label="账号">
        <el-input v-model="state.formInline.key" placeholder="模糊搜索"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="addBtn">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="state.tableData" style="width: 100%">
      <el-table-column prop="us" label="账号" width="180" />
      <el-table-column prop="sex" label="角色" width="180">
        <template #default="scope">
          {{ scope.row.roles[0].roleName }}
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="180">
        <template #default="scope">
          {{ scope.row.sex === 0 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template #default="scope">
          <el-switch v-model="scope.row.state" @change="stateChange($event, scope.row.id)" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" @click="assignRoles(scope.row)">分配角色</el-button>
          <el-popconfirm
            confirm-button-text="是"
            cancel-button-text="否"
            :icon="InfoFilled"
            icon-color="red"
            title="你确定要删除此项吗？"
            @confirm="remove(scope.row._id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="state.page.pageNo"
      :page-size="state.page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="state.page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <AddUpdate @reload="getList" ref="AddUpdateRef"></AddUpdate>
    <UsersRole @reload="getList" ref="UsersRoleRef"></UsersRole>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, ref, unref } from 'vue'
import { usersPage, usersUpdateState, usersDel } from '@/api/user'
import { ElMessage } from 'element-plus'
import AddUpdate from './UsersAddUpdate.vue'
import UsersRole from './UsersRole.vue'
import { InfoFilled } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'users',
  setup() {
    const AddUpdateRef = ref()
    const UsersRoleRef = ref()
    // reactive定义多个数据的响应式
    const state = reactive({
      formInline: {
        key: ''
      },
      tableData: [],
      page: {
        pageNo: 1,
        pageSize: 2,
        total: 0
      }
    })

    const getList = () => {
      const { pageNo, pageSize } = state.page
      const { key } = state.formInline
      usersPage({ pageNo, pageSize, key }).then((res) => {
        if (res.data.code === 200) {
          const temp = res.data
          const { data, pageNo, pageSize, total } = temp
          state.tableData = data
          state.page = {
            pageNo,
            pageSize,
            total
          }
        }
      })
    }

    const onSubmit = () => {
      state.page.pageNo = 1
      getList()
    }

    const addBtn = () => {
      unref(AddUpdateRef).state.open = true
    }

    const handleSizeChange = (val: number) => {
      state.page.pageSize = val
      getList()
    }

    const handleCurrentChange = (val: number) => {
      state.page.pageNo = val
      getList()
    }

    const assignRoles = (row: object) => {
      unref(UsersRoleRef).init(row)
    }

    const stateChange = (state: boolean, id: number) => {
      usersUpdateState({ id, state }).then((res) => {
        if (res.data.code === 200) {
          ElMessage.success('状态修改成功')
          getList()
        }
      })
    }

    const remove = (_id: string) => {
      usersDel({ _id }).then((res) => {
        if (res.data.code === 200) {
          ElMessage.success('删除成功')
          getList()
        }
      })
    }

    onMounted(() => {
      getList()
    })

    return {
      state,
      onSubmit,
      handleSizeChange,
      handleCurrentChange,
      assignRoles,
      addBtn,
      getList,
      AddUpdateRef,
      stateChange,
      remove,
      InfoFilled,
      UsersRoleRef
    }
  },
  components: { AddUpdate, UsersRole }
})
</script>

<style></style>
